var sysUser = require('../../modules/user/sysUser');
var express = require('express');
var session = require('express-session');
var router = express.Router();

module.exports = router;


router.get('/sysUser/fj/get', function (req, res) {
    //获取http://xxxx/sf/a.html?id=1
    var id = req.query.id;
    sysUser.get(id, function(values) {
        var data = [];
        if(values) {
            for(var i = 0; i < values.length; i++) {
                data.push(values[i].id, '\t', values[i].username, '\t', values[i].nickname);
            }
        }
        //res.writeHead(200, {'Content-Type': 'text/html'});
        //res.write('<head><meta charset="utf-8"/></head>');
        res.json(data);
    }, function(err) {
        res.json(global.result.error);
    });
});


router.post('/sysUser/login', function (req, res) {
    var username = req.body.username;
    var password = req.body.password;
    sysUser.login(username, password, function(data) {
        if(data.code === 0) {
            //登录成功
            console.log(data.body.username + '登录成功');
            session.user = data.body;
        }
        res.json(data);
    }, function(err) {
        res.json(global.result.error);
    });
});

router.get('/sysUser/fv/main', function(req, res) {
    var user = session.user;
    res.render('user/main', { user: user });
});